<template>
  <div class="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      class="tabControl"
      :titles="['流行','新款','精选']"
      @tabClick="tabClick"
      ref="tabControll1"
      v-show="isTabControllFixed"
      key="aaa"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :pullUpLoad="true"
      @pullingUp="loadMore"
      @scroll="contentScroll"
    >
      <home-swiper :banners="banners" @swiperImgLoaded="swiperImgLoaded" />
      <recommend-view :recommend="recommend" />
      <feature-view />
      <tab-control :titles="['流行','新款','精选']" @tabClick="tabClick" ref="tabControll2" key="bbb" />
      <goods-list :goods="showGoods" />
    </scroll>
    <back-top @click.native="goTop" v-show="isShowBackTop" />
  </div>
</template>


<script>
import { getHomeMultidata, getHomeGoods } from "network/home";
import { itemListenerMixin,backTopMixin } from "common/mixin";

import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView.vue";

import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabcontrol/TabControl";
import GoodsList from "components/content/goodsList/GoodsList";
import Scroll from "components/common/scroll/Scroll";

export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommend: [],
      goods: {
        pop: { page: 0, list: [] }, // 流行
        new: { page: 0, list: [] }, // 新款
        sell: { page: 0, list: [] }, // 精选
      },
      currentType: "pop",
      tabBarOffsetTop: 0,
      isTabControllFixed: false,
      saveY: 0,
    };
  },
  mixins: [itemListenerMixin,backTopMixin],
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll
  },
  activated() {
    // 进入时滚动到离开时的位置this.saveY
    this.$refs.scroll.refresh(); // 进入时再刷新，避免出现小问题
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  deactivated() {
    // 保存离开时的位置信息到this.saveY
    this.saveY = this.$refs.scroll.getScrollY();
    // console.log(this.saveY);
    // 2.取消全局事件监听（主页图片加载的监听）因为此时设置了keep-alive，所以离开时调用的是deactivated()
    this.$bus.$off("itemImageLoad", this.itemImgListener);
  },
  created() {
    this.getHomeMultidata(); // 获取首页轮播图和推荐数据
    this.getHomeGoods("pop"); //获取首页流行数据
    this.getHomeGoods("new"); //获取首页新款数据
    this.getHomeGoods("sell"); //获取首页精选数据
  },
  mounted() {
    /**
     * 在组件加载完成后立刻去监听图片加载完成的事件
     * 正常的调用非常的频繁，这里添加防抖
     */
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  methods: {
    /**
     * 网络相关的方法
     *
     */
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        const { banner, recommend } = res.data;
        this.banners = banner.list;
        this.recommend = recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;

      getHomeGoods(type, page).then((res) => {
        this.goods[type].list = this.goods[type].list.concat(res.data.list);
        this.goods[type].page += 1;

        this.$refs.scroll.finishPullUp();
      });
    },

    /**
     * 事件监听相关的方法
     *
     */
    tabClick(index) {
      this.currentType = Object.keys(this.goods)[index];

      this.$refs.tabControll1.currentIndex = index;
      this.$refs.tabControll2.currentIndex = index;
      this.$refs.scroll.refresh();
    },
    contentScroll(position) {
      // 1. 判断是否回到顶部
      const { y } = position;
     this.listenerBackTop(position)

      // 2. 判断是否给tab-controll吸顶效果
      this.saveY = -y;

      if (-y > this.tabBarOffsetTop) {
        this.isTabControllFixed = true;
      } else {
        this.isTabControllFixed = false;
      }
    },
    loadMore() {
      this.getHomeGoods(this.currentType);
      this.$refs.scroll && this.$refs.scroll.refresh();
    },
    swiperImgLoaded() {
      //1. 当轮播图加载完之后拿到tab-controll的offset-top值
      this.tabBarOffsetTop = this.$refs.tabControll2.$el.offsetTop;
    },
  },
  watch: {
    currentType() {
      console.log("tab栏切换执行refresh");
      this.$refs.scroll.refresh();
    },
  },
};
</script>


<style scoped>
.home {
  height: 100vh;
  position: relative;
  /* padding-top: 44px; */
}
.home-nav {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9;
  color: #fff;
  background-color: var(--color-tint);
}
.tabControl {
  /**
    position: sticky;当我们没有满足条件的时候，他是以position:static展示,
    当它满足条件的时候，他会以position:fixed来展示
  */
  position: relative;
  top: 44px;
}
.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

/* .content {
  height: calc(100vh - 93px);
  margin-top: 44px;
  overflow: hidden;
} */
</style>